package org.caro.gui;

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

import org.caro.actions.ContestantAction;
import org.caro.actions.listener.AiModeExecute;
import org.caro.actions.listener.LanModeExecute;
import org.caro.utils.Constants;

public class ActionZonePanel extends JPanel  {

    private JButton button[][];
    private int mapButton[][]; // bang so tuong ung
    private UtilityZonePanel utilityZone;
    private ContestantAction contestant;
    
    
    public JButton[][] getButton() {
		return button;
	}

	public void setButton(JButton[][] button) {
		this.button = button;
	}

	public int[][] getMapButton() {
		return mapButton;
	}

	public void setMapButton(int[][] mapButton) {
		this.mapButton = mapButton;
	}

	/**
     *
     */
    private static final long serialVersionUID = 1L;

    public ActionZonePanel(int mode,UtilityZonePanel utilityZone,ContestantAction contestant) {
        this.utilityZone = utilityZone;
        this.contestant = contestant;
        button = new JButton[Constants.MAX_ROWS][Constants.MAX_COLUMNS];
        mapButton = new int[Constants.MAX_ROWS][Constants.MAX_COLUMNS];
        setLayout(new GridLayout(Constants.MAX_ROWS, Constants.MAX_COLUMNS));
        for (int i = 0; i < Constants.MAX_ROWS; i++) {
            for (int j = 0; j < Constants.MAX_COLUMNS; j++) {
                button[i][j] = new JButton();
                add(button[i][j]);
            }
        }
        if (mode == Constants.AI_MODE) addAIActionListener();
        else addLANActionListener();
    }

    private void addAIActionListener(){
    	AiModeExecute aiMode = new AiModeExecute(this,utilityZone);
    	for (int i = 0; i < Constants.MAX_ROWS; i++) {
            for (int j = 0; j < Constants.MAX_COLUMNS; j++) {
                button[i][j].addActionListener(aiMode);
            }
        }
    }
    
    private void addLANActionListener(){
    	LanModeExecute lanMode = new LanModeExecute(this,utilityZone,contestant);
    	for (int i = 0; i < Constants.MAX_ROWS; i++) {
            for (int j = 0; j < Constants.MAX_COLUMNS; j++) {
                button[i][j].addActionListener(lanMode);
            }
        }
    }

    public void setDefaultBoard() {
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                button[i][j].setIcon(null);
                mapButton[i][j] = Constants.NULL;
            }
        }
    }
     
}
